package com.fzy.core.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.fzy.core.utils.BooleanUtils;
import com.fzy.core.utils.StringUtils;

public class ExtPanelTag extends BodyTagSupport {

	private static final long serialVersionUID = -6077400408031503961L;

	String hashead;
	String head;
	String width;
	String height;
	String style;

	public int doStartTag() throws JspException {
		width = StringUtils.defaultIfEmpty(width, "100%");
		
		String html = "";
		html += "<div style=\"width: "+width+";\" class=\"x-panel x-panel-default my-panel\">";
		boolean hasHead = BooleanUtils.toBoolean(hashead, true);
		if(hasHead){
			String basePath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
			html += "	<div";
			html += "		class=\"x-panel-header x-panel-header-default x-panel-header-horizontal x-docked-top x-panel-header-default-top\"";
			html += "		style=\"overflow: auto;\">";
			html += "		<div class=\"x-panel-header-body x-panel-header-body-default\">";
			html += "			<table style=\"width: 100%;\">";
			html += "				<tr>";
			html += "					<td>";
			html += "						<div style=\"text-align: left;\"";
			html += "							class=\"x-panel-header-text-container\">";
			html += "							<span";
			html += "								class=\"x-panel-header-text x-panel-header-text-default\">"+StringUtils.defaultString(head)+"</span>";
			html += "						</div>";
			html += "					</td>";
			html += "					<td style=\"width: 15px;\">";
			html += "						<div id=\"left_menu_icon\" style=\"width: 15px; height: 15px;\"";
			html += "							class=\"x-tool x-tool-default\">";
			html += "							<img class=\"x-tool-collapse-top\"";
			html += "								src=\""+basePath+"/images/transparent.gif\">";
			html += "						</div>";
			html += "					</td>";
			html += "				</tr>";
			html += "			</table>";
			html += "		</div>";
			html += "	</div>";
		}
		html += "	<div class=\"x-panel-body x-panel-body-default x-grid-body\" style=\"padding: 5px;\">";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		BodyContent bodycontent = getBodyContent();
		String html = bodycontent.getString();
		try {
			bodycontent.getEnclosingWriter().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doAfterBody();
	}

	public int doEndTag() throws JspException {
		String html = "";
		html += "	</div>";
		html += "</div>";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public String getHashead() {
		return hashead;
	}

	public void setHashead(String hashead) {
		this.hashead = hashead;
	}

	public String getHead() {
		return head;
	}

	public void setHead(String head) {
		this.head = head;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

}
